forum

home / developersection / forums / class field become a tag name using jaxb

class field become a tag name using JAXB

Anonymous User 2273 16-Oct-2014

I am using Java and JAXB for XML processing.

I have the following class:

    public class Characteristic {
 
        private String characteristic;
        private String value;
 
        @XmlAttribute
        public String getCharacteristic() {
            return characteristic;
        }
 
        public void setCharacteristic(String characteristic) {
            this.characteristic = characteristic;
        }
 
        @XmlValue
        public String getValue() {
            return value;
        }
 
        public void setValue(String value) {
            this.value = value;
        }
    }
 
    public static void main(String[] args) {
        Characteristic c = new Characteristic();
        c.setCharacteristic("store_capacity");
        c.setValue(40);
        Characteristic c2 = new Characteristic();
        c2.setCharacteristic("number_of_doors");
        c2.setValue(4);
    }

This is the result that I get:

<characteristics characteristic="store_capacity">40</characteristics>
<characteristics characteristic="number_of_doors">4</characteristics>

I want to get the following result:

<store_capacity>40</store_capacity>
<number_of_doors>4</number_of_doors>

How can I achieve this?


java xml  java 
Updated on 16-Oct-2014

I am a content writter !

Can you answer this question?

Answer

1 Answers

Liked By